<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\model\OrderReviewsModel;
use app\model\OrdersModel;
use app\model\PostCommentsModel;
use app\model\PostsModel;
use app\model\PostUserLikesModel;
use app\model\BasePostLabelsModel;
use app\model\PostWhiteUserModel;
use app\model\ProductsModel;
use app\model\UserModel;
use think\App;
use think\facade\Db;

class Posts extends Base {
    private $req;

    private $uid;

    private $postsModel;

    private $postCommentsModel;

    private $postUserLikesModel;

    private $page;

    private $limit = 10;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page']  : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit']  : 10;

        $this->postsModel = new PostsModel();

        $this->postCommentsModel = new PostCommentsModel();

        $this->postUserLikesModel = new PostUserLikesModel();

        $this->userModel = new UserModel();

        $this->uid = $this->isLogin();
    }



    /*
     * 获取评价列表
     * */

    public function getList()
    {
        $where=[];

        $where[] = ['post_status','=',1];

        $list = $this->postsModel->getList($where,$this->page,$this->limit,["create_at"=>"DESC","views"=>"DESC"]);

        $total = $this->postsModel->getTotal($where);

        return $this->succData($total,$list);
    }

    /*
        * 获取评价列表
        * */

    public function getUserPostList()
    {
        $where=[];
        $where[] = ['uid','=',$this->uid];

        $list = $this->postsModel->getList($where,$this->page,$this->limit,["create_at"=>"DESC","likes"=>"DESC"]);

        $total = $this->postsModel->getTotal($where);

        return $this->succData($total,$list);
    }


    /*
     * 发帖
     *
     * */
    public function publishPost(){

        if(!(new PostWhiteUserModel())->isWhiteUser($this->uid)){
            return $this->fail("仅限白名单用户发帖");
        }

        if(!$this->req["content"]){
            return $this->fail("内容不能为空");
        }

        if(!empty($this->req["images"]) && !is_array($this->req["images"])){
            return $this->fail("请选择要上传的图片");
        }

        if(!empty($this->req["images"]) && count($this->req["images"]) > 9){
            return $this->fail("图片最多只能传9张");
        }

        if(!$this->req["label_id"]){
            return $this->fail("请选择标签");
        }


        $res = $this->postsModel->addOne([
            "uid"=>$this->uid,
            "label_id"=>$this->req["label_id"],
            "content"=>$this->req["content"],
            "images"=> (isset($this->req["images"]) && is_array($this->req["images"])) ? json_encode($this->req["images"],256) : "",
        ]);

        if($res){
            return $this->succ("已提交审核，请耐心等待");
        }else{
            return $this->fail("发布失败");
        }
    }



    /*
    * 发帖
    *
    * */
    public function updatePost(){

        if(!(new PostWhiteUserModel())->isWhiteUser($this->uid)){
            return $this->fail("仅限白名单用户完成此操作");
        }

        if(!$this->req["post_id"]){
            return $this->fail("帖子Id不能为空");
        }

        if(!$this->req["content"]){
            return $this->fail("内容不能为空");
        }

        if(!empty($this->req["images"]) && !is_array($this->req["images"])){
            return $this->fail("请选择要上传的图片");
        }

        if(!empty($this->req["images"]) && count($this->req["images"]) > 9){
            return $this->fail("图片最多只能传9张");
        }

        if(!$this->req["label_id"]){
            return $this->fail("请选择标签");
        }

        $postInfo = $this->postsModel->getInfo($this->req["post_id"]);

        if($postInfo["uid"] != $this->uid){
            return $this->fail("只能操作自己的帖子");
        }

        $res = $this->postsModel->updateOne($this->req["post_id"],[
            "content"=>$this->req["content"],
            "label_id"=>$this->req["label_id"],
            "images"=> (isset($this->req["images"]) && is_array($this->req["images"])) ? json_encode($this->req["images"],256) : "",
            "post_status"=>0,
            ]);

        $this->postsModel->delCache($this->req["post_id"]);

        if($res){
            return $this->succ("已提交审核，请耐心等待");
        }else{
            return $this->fail("修改失败");
        }
    }



    /*
    * 获取指定帖子详情
    * */
    public function getPostInfo(){
        if(!$this->req["post_id"]){
            return $this->fail("帖子Id不能为空");
        }

        //新增浏览量
        $this->postsModel->incrField($this->req["post_id"],"views");

        $res = $this->postsModel->getInfo($this->req["post_id"]);

        $res["haveLike"] = $this->postUserLikesModel->isPostLike($this->uid,$this->req["post_id"]);

        $this->postsModel->delCache($this->req["post_id"]);

        if($res){
            return $this->succ($res);
        }else{
            return $this->fail();
        }
    }


    /*
     * 获取帖子的评论
     * */

    public function getPostComments()
    {
        if(!$this->req["post_id"]){
            return $this->fail("帖子Id不能为空");
        }

        $where=[];
        $where[] = ['post_id','=',$this->req["post_id"]];
        $where[] = ['parent_comment_id','=',0];

        $total = $this->postCommentsModel->getTotal($where);

        $list = $this->postCommentsModel->getList($where,$this->page,$this->limit,["create_at"=>"DESC","likes"=>"DESC"]);

        if($list){
            $postInfo = $this->postsModel->getInfo($this->req["post_id"]);
            foreach ($list as &$item){
                $item["isAuthor"] =  0;
                if($item["uid"] == $postInfo["uid"]){
                    $item["isAuthor"] = 1;
                }
            }
        }


        return $this->succData($total,$list);

    }

    /*
       * 获取评论的评论列表
       * */
    public function getCommentComments()
    {
        if(!$this->req["comment_id"]){
            return $this->fail("评论Id不能为空");
        }

        $where=[];
        $where[] = ['parent_comment_id','=',$this->req["comment_id"]];

        $total = $this->postCommentsModel->getTotal($where);

        $list = $this->postCommentsModel->getList($where,$this->page,$this->limit,["create_at"=>"DESC","likes"=>"DESC"]);

        $commentInfo = $this->postCommentsModel->getInfo($this->req["comment_id"]);
        $postInfo = $this->postsModel->getInfo($commentInfo["post_id"]);
        foreach ($list as &$item){
            $item["isAuthor"] =  0;
            if($item["uid"] == $postInfo["uid"]){
                $item["isAuthor"] = 1;
            }
        }

        return $this->succData($total,$list);

    }



    /*
   * 点赞帖子
   * */
    public function doLikePost(){

        if(!$this->req["post_id"]){
            return $this->fail("帖子Id不能为空");
        }

        Db::startTrans();

        if($this->postUserLikesModel->isPostLike($this->uid,$this->req["post_id"])){

            $res1 = $this->postUserLikesModel->where([
                "uid"=>$this->uid,
                "post_id"=>$this->req["post_id"],
            ])->delete();

            $res2 = $this->postsModel->decField($this->req["post_id"],"likes");

        }else{
            $res1 =  $this->postUserLikesModel->addOne([
                "uid"=>$this->uid,
                "post_id"=>$this->req["post_id"],
            ]);

            $res2 = $this->postsModel->incrField($this->req["post_id"],"likes");

        }


        $this->postUserLikesModel->delPostLike($this->uid,$this->req["post_id"]);

        if($res1 && $res2){
            Db::commit();

            $this->postsModel->delCache($this->req["post_id"]);

            return $this->succ("成功");
        }else{
            Db::rollback();
            return $this->fail("失败");
        }
    }


    /*
     * 点赞评论
     * */
    public function doLikePostComments(){

        if(!$this->req["post_id"]){
            return $this->fail("帖子Id不能为空");
        }

        if(!$this->req["comment_id"]){
            return $this->fail("评论Id不能为空");
        }

        Db::startTrans();

        if($this->postUserLikesModel->isPostCommentLike($this->uid,$this->req["comment_id"])){

            $res1 = $this->postUserLikesModel->where([
                "uid"=>$this->uid,
                "post_id" => $this->req["post_id"],
                "comment_id" => $this->req["comment_id"],
            ])->delete();

            $res2 = $this->postCommentsModel->decField($this->req["comment_id"],"likes");

        }else {

            $res1 = (new PostUserLikesModel())->addOne([
                "uid" => $this->uid,
                "post_id" => $this->req["post_id"],
                "comment_id" => $this->req["comment_id"],
            ]);

            $res2 = $this->postCommentsModel->incrField($this->req["comment_id"], "likes");
        }

        $this->postUserLikesModel->delPostCommentLike($this->uid,$this->req["comment_id"]);


        if($res1 && $res2){

            Db::commit();

            $this->postCommentsModel->delCache($this->req["comment_id"]);

            return $this->succ("成功");
        }else{
            Db::rollback();

            return $this->fail("失败");
        }
    }




    public function publishComment(){

        if(!$this->req["post_id"]){
            return $this->fail("帖子Id不能为空");
        }


        if(!$this->req["content"]){
            return $this->fail("内容不能为空");
        }


        Db::startTrans();

        $res1 = $this->postCommentsModel->addOne([
            "uid"=>$this->uid,
            "post_id"=>$this->req["post_id"],
            "content"=>$this->req["content"],
            "parent_comment_id"=> isset($this->req["comment_id"]) ? $this->req["comment_id"] : 0,
            //"images"=> (isset($this->req["images"]) && is_array($this->req["images"])) ? json_encode($this->req["images"],256) : "",
        ]);

        $res2 = $this->postsModel->incrField($this->req["post_id"], "comments");


        if($res1 && $res2){

            Db::commit();

            $this->postsModel->delCache($this->req["post_id"]);

            return $this->succ("发表成功");

        }else{

            Db::rollback();

            return $this->fail("发表失败");
        }
    }



    /*
     * 删除帖子
     * */

    public function delPost(){

        if(!$this->req["post_id"]){
            return $this->fail("帖子Id不能为空");
        }

        $res = $this->postsModel->where([
            "uid"=>$this->uid,
            "post_id"=>$this->req["post_id"],
        ])->delete();

        if($res){
            return $this->succ("删除成功");
        }else{
            return $this->fail("删除失败");
        }
    }


    /*
   * 删除帖子
   * */

    public function delPostComment(){

        if(!$this->req["comment_id"]){
            return $this->fail("评论Id不能为空");
        }


        Db::startTrans();

        $res1 = $this->postCommentsModel->where([
            "uid"=>$this->uid,
            "comment_id"=>$this->req["comment_id"],
        ])->delete();


        $res2 = $this->postsModel->decField($this->req["post_id"], "comments");


        if($res1 && $res2){
            Db::commit();

            $this->postsModel->delCache($this->req["post_id"]);

            return $this->succ("删除成功");
        }else{

            Db::rollback();

            return $this->fail("删除失败");
        }
    }


}
